package com.hnucm.cooklife.model;

import com.hnucm.cooklife.base.BaseModel;
import com.hnucm.cooklife.bean.db.User;
import com.hnucm.cooklife.contract.friends.Contract;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public class FriendModel extends BaseModel implements Contract.IFriendModel {
    public FriendModel() {
        setCookies(false);
    }

    @Override
    public Observable<User> loadFriend(int userId) {
        return mApiServer.loadUser(userId)
                .map(userData -> {
                    User user = new User();
                    user.userId = userData.getData().getUserId();
                    user.username = userData.getData().getUsername();
                    user.password = userData.getData().getPassword();
                    user.icon = userData.getData().getIcon();
                    user.bg = userData.getData().getBg();

                    System.out.println(user);

//                    if(user.id !=0){
//                        LitePal.deleteAll(User.class);
//                    }
//                    List<User> userList = new ArrayList<>();
//                    userList.add(user);
//                    LitePal.saveAll(userList);
//                    System.out.println(user);
                    return user;
                });
    }

    @Override
    public Observable<User> refreshFriend(int userId) {
        return loadFriend(userId);
    }
}
